package com.kuang.snake;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;

public class GamePanel extends JPanel implements KeyListener, ActionListener {

    int lenth; //蛇的长度
    int[] snakeX = new int[600];
    int[] snakeY = new int[500];
    String fx; //R:右 L:左 U:上 D:下
    int foodX = 0;
    int foodY = 0;
    int score = 0;

    boolean isStart = false; //游戏是否开始
    boolean isFail = false; //死亡失败
    boolean inBody = false;//食物是否初始化在身体内

    Random rand = new Random();


    Timer timer = new Timer(100, this); // 控制程序启动以及刷新频率（delay）

    //构造器
    public GamePanel(){
        init();

        //获取键盘的监听事件
        this.setFocusable(true);
        this.addKeyListener(this);
        timer.start();
    }


    //初始化
    public void init(){
        lenth = 3;
        snakeX[0] = 100; snakeY[0] = 100; //头部坐标
        snakeX[1] = 75; snakeY[1] = 100; //第一个身体坐标
        snakeX[2] = 50; snakeY[2] = 100; //第二个身体坐标
        fx = "R";

        foodX = 25 + 25*rand.nextInt(33);
        foodY = 75 + 25*rand.nextInt(23);
    }


    //画板：画界面，画蛇
    //只执行画的功能，不要进行赋值功能（赋值功能在键盘监听和计时器动作监听里写）
    //Graphics: 画笔
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);//清屏
        this.setBackground(Color.BLACK);

        //绘制头部的广告栏
        Data.header.paintIcon(this, g, 25, 11);

        g.fillRect(25, 75, 850, 600); //起始X坐标，起始Y坐标，宽度，高度。

        //绘制分数和长度
        g.setColor(Color.RED);
        g.setFont(new Font("微软雅黑", Font.BOLD, 22));
        g.drawString("分数："+String.valueOf(score), 750, 35);
        g.drawString("长度："+String.valueOf(lenth), 750, 55);

        //画一条静态的小蛇
        if(fx.equals("R")){
            Data.right.paintIcon(this, g, snakeX[0], snakeY[0]);
        } else if(fx.equals("L")){
            Data.left.paintIcon(this, g, snakeX[0], snakeY[0]);
        } else if(fx.equals("U")) {
            Data.up.paintIcon(this, g, snakeX[0], snakeY[0]);
        } else if(fx.equals("D")) {
            Data.down.paintIcon(this, g, snakeX[0], snakeY[0]);
        }

        Data.food.paintIcon(this, g, foodX, foodY);

        for (int i = 1; i < lenth; i++){
            Data.body.paintIcon(this, g, snakeX[i], snakeY[i]);
        }


        //游戏提示：是否开始
        if (isStart==false && isFail == false){
            //画一个文字，string
            g.setColor(Color.WHITE);
            g.setFont(new Font("微软雅黑", Font.BOLD, 40));
            g.drawString("按下空格开始游戏", 300, 300);
        }

        if(isFail == true){
            g.setColor(Color.red);
            g.setFont(new Font("微软雅黑", Font.BOLD, 40));
            g.drawString("游戏结束", 350, 300);
        }



    }



    //接受键盘的输入：监听
    @Override
    public void keyPressed(KeyEvent e) {
        //键盘按下，未释放
        int keyCode = e.getKeyCode();

        if (keyCode==KeyEvent.VK_SPACE){

            if (isFail == true){
                isFail = false;
                init();
            }else{
                isStart = !isStart;
            }
            repaint(); //刷新界面
        }



        if (keyCode==KeyEvent.VK_LEFT){
            if (fx != "R"){
                fx="L";
            }
        }else if(keyCode==KeyEvent.VK_RIGHT){
            if (fx != "L"){
                fx="R";
            }
        }else if(keyCode==KeyEvent.VK_UP){
            if (fx != "D"){
                fx="U";
            }
        }else if(keyCode==KeyEvent.VK_DOWN){
            if (fx != "U"){
                fx="D";
            }
        }
    }


    //定时器，监听时间，帧：执行定时操作
    @Override
    public void actionPerformed(ActionEvent e) {
        //如果游戏处于开始状态，并且没有结束
        if (isStart && isFail==false){
            //右移
            for (int i = lenth-1; i > 0; i--){ //除了脑袋，身体前移
                snakeX[i] = snakeX[i-1];
                snakeY[i] = snakeY[i-1];
            }

            if (fx=="L"){
                snakeX[0] = snakeX[0] - 25; //头部向左移动
                if(snakeX[0]<25){snakeX[0] = 850;}
            }else if(fx=="R"){
                snakeX[0] = snakeX[0] + 25; //头部向右移动
                if(snakeX[0]>850){snakeX[0] = 25;}
            }else if(fx=="U"){
                snakeY[0] = snakeY[0] - 25; //头部向上移动
                if(snakeY[0]<75){snakeY[0] = 625;}
            }else if(fx=="D"){
                snakeY[0] = snakeY[0] + 25; //头部向下移动
                if(snakeY[0]>625){snakeY[0] = 75;}

            }

            if (foodX == snakeX[0] && foodY == snakeY[0]){
                score++;
                lenth++;
                snakeX[lenth-1]=999999; //新加尾巴先放在屏幕外
                snakeY[lenth-1]=999999;

                foodX = 25 + 25*rand.nextInt(33);
                foodY = 75 + 25*rand.nextInt(23);
                inBody = isFoodInBody();
                while(inBody){
                    inBody = isFoodInBody();
                }
            }



            for (int i = 1; i < lenth; i++){
                if(snakeX[i]==snakeX[0] && snakeY[i]==snakeY[0]){
                    isFail = true;
                }
            }

            //刷新界面
            repaint();
        }
        timer.start();
    }

    public boolean isFoodInBody()
    {
        for (int i = 1; i < lenth; i++){
            if(snakeX[i]==foodX && snakeY[i]==foodY){
                inBody =true;
            }
        }

        return inBody;
    }

    @Override
    public void keyTyped(KeyEvent e) {
        //键盘按下弹起
    }

    @Override
    public void keyReleased(KeyEvent e) {
        //键盘释放
    }
}
